package org.example.managementsystem.repository;
import org.example.managementsystem.model.Production_Review;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Objects;
@Repository
public class Production_ReviewDao {
    private final JdbcTemplate jdbcTemplate;
    @Autowired
    public Production_ReviewDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public int insertProduction_Review(Production_Review production_review) {
        String sql = "INSERT INTO production_review ( submission_id, description, is_approved) " +
                "VALUES (?, ?, ?)";

        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(connection -> {
            PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            ps.setInt(1, production_review.getSubmission_id());
            ps.setString(2, production_review.getDescription());
            ps.setBoolean(3, production_review.isIs_approved());

            return ps;
        }, keyHolder);
        return Objects.requireNonNull(keyHolder.getKey()).intValue();
    }
    public int updateProduction_Review(Production_Review production_review) {
        String sql = "UPDATE production_review SET  description = ?, is_approved = ? " +
                "WHERE submission_id = ?";

        return jdbcTemplate.update(sql,

                production_review.getDescription(),
                production_review.isIs_approved(),
                production_review.getSubmission_id()
        );

    }
    public Production_Review getProduction_ReviewById(int submission_id) {
        String sql = "SELECT * FROM production_review WHERE submission_id = ?";
        List<Production_Review> production_review = jdbcTemplate.query(sql, new Production_ReviewRowMapper(), submission_id);
        return production_review.isEmpty() ? null : production_review.get(0);
    }
    public int deleteProduction_Review(int submission_id) {
        String sql = "DELETE FROM production_review WHERE submission_id = ?";
        return jdbcTemplate.update(sql, submission_id);
    }

    public List<Production_Review> getAllProduction_Review() {
        String sql = "SELECT * FROM production_review";
        return jdbcTemplate.query(sql, new Production_ReviewRowMapper());
    }
    private static class Production_ReviewRowMapper implements RowMapper<Production_Review> {
        @Override
        public Production_Review mapRow(ResultSet rs, int rowNum) throws SQLException {
            Production_Review production_review = new Production_Review();
            production_review.setSubmission_id(rs.getInt("submission_id"));
            production_review.setIs_approved(rs.getBoolean("is_approved"));
            production_review.setDescription(rs.getString("description"));

            return production_review;
        }
    }
}
